package com.example.easyutils.joke;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.easyutils.R;

import java.util.List;
import java.util.Map;

public class JokeRecycleView extends RecyclerView.Adapter {

    private List mList;
    private Context context;
    public JokeRecycleView(List list, Context context) {
        this.mList = list;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.joke_item,parent,false);
        ViewHoder viewHoder = new ViewHoder(view);
        return viewHoder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((ViewHoder) holder).textView.setText(((Map)mList.get(position)).get("title").toString());
        Glide
                .with(context)
                .load(((Map)mList.get(position)).get("img").toString())
                .into(((ViewHoder) holder).imageView);


    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHoder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView textView;
        public ViewHoder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.joke_image);
            textView = itemView.findViewById(R.id.joke_title);
        }
    }
}
